<?php
namespace app\admin\logic;

class Article extends Base
{
    public function jumpCommon()
    {
        $class = new \app\common\logic\Article();
        return $class;
    }

    public function jumpIndex()
    {
        $class = new \app\index\logic\Article();
        return $class;
    }

    public function add($pm)
    {
        $pm['create_time'] = time();
        $pm['describe'] = mb_substr(strip_tags($pm['content_html']), 0, 100);
        $id = db('article')->strict(false)->insertGetId($pm);
        $data['tag'] = $pm['tags'];
        $data['article_id'] = $id;
        $this->logic()->tag()->addArticleTagMaybeAddTag($data);
        return ['id'=>$id];
    }


    public function detail($pm)
    {
        $info = db('article')->where('id', $pm['id'])->find();
        return $info;
    }


    public function edit($pm)
    {
        $condition = isset($pm['condition'])?$pm['condition']:'';
        if (isset($pm['id']) && !$condition) {
            $condition = 'id = '.$pm['id'];
        }
        $rt = $this->allowField(true)->save($pm, $condition);
        return $rt;
    }

    public function del($pm)
    {
        $condition = isset($pm['condition'])?$pm['condition']:'';
        if (isset($pm['id']) && !$condition) {
            $condition = 'id = '.$pm['id'];
        }
        $rt = db('article')->where($condition)->update(['is_del'=>1, 'edit_time'=>time()]);
        return $rt;
    }

    public function getTables($pm)
    {
        // 条件部分
        $where = [];
        if (!empty($pm['title'])) {
            $where[] = "title like '%{$pm['title']}%'";
        }
        if (!empty($pm['sort_id'])) {
            $where[] = 'sort_id = '.$pm['sort_id'];
        }
        if (!empty($pm['create_date_min'])) {
            $where[] = 'create_time >= '.strtotime($pm['create_date_min']);
        }
        if (!empty($pm['create_date_max'])) {
            $where[] = 'create_time <= '.strtotime($pm['create_date_max']);
        }
        if (!empty($pm['edit_date_min'])) {
            $where[] = 'edit_time >= '.strtotime($pm['edit_date_min']);
        }
        if (!empty($pm['edit_date_max'])) {
            $where[] = 'edit_time <= '.strtotime($pm['edit_date_max']);
        }
        $conditon = isset($pm['condition'])?$pm['condition']:[];
        // 数据 总数
        $list = db('article')->where('is_del = 0')->where($where)->where($conditon)->page($pm['page'], $pm['limit'])->select();
        $count = db('article')->where('is_del = 0')->where($where)->where($conditon)->count();
        foreach ($list as &$item) {
            $item['sort_name'] = $this->logic()->articleSort()->getNameById($item['sort_id']);
            $TagArr = $this->logic()->tag()->getNamesByIds($item['tags']);
            $item['tags_name'] = implode(',', $TagArr);
            $item['create_date'] = date('Y-m-d H:i:s', $item['create_time']);
            $item['edit_date'] = $item['edit_time'] ? date('Y-m-d H:i:s', $item['edit_time']) : '-';
        }
        return ['list'=>$list,'count'=>$count];
    }

}